#include "client.h"

int transOptToNum(char* opt,int* num){
    if(strcmp(opt,"cd")==0){
        *num=0;
    }
    else if(strcmp(opt,"ls")==0){
        *num=1;
    }
    else if(strcmp(opt,"puts")==0){
        *num=2;
    }
    else if(strcmp(opt,"gets")==0){
        *num=3;
    }
    else if(strcmp(opt,"remove")==0){
        *num=4;
    }
    else if(strcmp(opt,"pwd")==0){
        *num=5;
    }
    else if(strcmp(opt,"mkdir")==0){
        *num=6;
    }
    else if(strcmp(opt,"rmdir")==0){
        *num=7;
    }
    else if(strcmp(opt,"quit")==0){
        *num=8;
    }
    else{
        printf("不存在此种操作\n");
        *num=-1;
    }
    return *num;//-1 表示没有这种命令
}
//删除前部空格
int removeSpace(char* str){
    int i=0,j=0;
    while(str[i]==' '){
        i++;
    }
    if(i>0){
        while(str[i]!='\0'){
            str[j++]=str[i++];
        }
        str[j]='\0';
        return 1;
    }
    return 0;
}

void removeFirstToken(char* str){
    int i=0,j=0;
    int len=strlen(str);
    for(;i<len;i++){
        if(str[i]==' '){

            break;
        }
    }
    for(;i<len;i++){

        if(str[i]!=' '){
            break;
        }
    }
    while(str[i]!='\0'){
        str[j++]=str[i++];
    }
    str[j+1]='\0';
    str[j]='\0';
}
int main(int argc,char* argv[]){
    //./client 192.168.88.129 1234
    if(argc<3){
        error(1,0,"参数缺失\n");
    }
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(atoi(argv[2]));
    addr.sin_addr.s_addr=inet_addr(argv[1]);
    int ret=connect(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    if(ret==-1){
        error(1,errno,"connect is error\n");
    }
    ret=loginFunc(sockfd);
    if(ret!=0){
        printf("有误\n");
    }
    else{
        printf("登录成功\n");
    }

    train_t t_command;//用来存储命令
    while(1){
        fflush(stdout);//刷新输出缓冲区
        bzero(&t_command,sizeof(t_command));
        fgets(t_command.data,4096,stdin);//获取输入内容

        //
        removeSpace(t_command.data);
       // t_command.data[strlen(t_command.data)-1]=' ';
       // t_command.data[strlen(t_command.data)]=' ';
       // printf("%s\n",t_command.data);
       char temp[4096];
       strcpy(temp,t_command.data);
       char* token=strtok(temp," ");//取出命令opt
       if(token==NULL){
           printf("没有此操作\n");
           continue;
       }
       char opt[1024];
       strcpy(opt,token);//复制操作命令
       removeFirstToken(t_command.data);
       //获取文件名
       t_command.length=strlen(t_command.data);
       
       int optnum=99;
       int retoptnum=transOptToNum(opt,&optnum);//转为命令数字
       if(retoptnum==-1){
           printf("未知操作\n");
           continue;
       }
       send(sockfd,&optnum,sizeof(int),0);
       send(sockfd,&t_command,sizeof(t_command.length)+t_command.length,0);
       printf("%d %s\n",t_command.length,t_command.data);
       printf("arg:%s\n",t_command.data);
       printf("optnum= %d",optnum);
       
       //获取文件名
       char filename[256];
       //char* filep=strrchr(t_command.data,'/');
       //if(filep!=NULL){
       //    filep++;
       //}
       //else{

       //    filep=t_command.data;
       //}
       strcpy(filename,t_command.data);
       filename[strlen(filename)]='\0';
       printf("client filename= %s\n",filename);
       int ifError;
       switch(optnum){
       case 0:
           //cd
           printf("正在执行cdfunc\n");
           cdFunc(sockfd,t_command.data);
           break;
       case 1:
           //ls
           lsFunc(sockfd);
           break;
       case 2:
           //2
           //puts
           putsFunc(sockfd,filename);
           
           break;
       case 3:
           //gets
           break;
        case 4:
           //remove
           removeFunc(sockfd);
           break;
        case 5:
           //pwd
           pwdFunc(sockfd);
           break;
        case 6:
           //mkdir
           mkdirFunc(sockfd);
           break;
        case 7:
           //rmdir
           rmdirFunc(sockfd);
           break;
        case 8:
           //recv
           break;
        default:
           recv(sockfd,&ifError,sizeof(int),0);
           printf("没有此操作\n");

       }

    }
    close(sockfd);
    return 0;
}
